#ifndef __WIZ_BASE_UTIL_SINGLETON_NON_INSTRUSIVE_HPP__SHANHAOBO_19800429__
#define __WIZ_BASE_UTIL_SINGLETON_NON_INSTRUSIVE_HPP__SHANHAOBO_19800429__

#include "../Type/WizBaseType.hpp"

#include "./WizBaseUtilsNoncopyable.hpp"

namespace Wiz
{
    /// Non Instrusive Singleton
    namespace NISingleton
    {
        namespace Base
        {
            class Type : public ::Wiz::Noncopyable::Type
            {
            private:
                static bool & GetLock()
                {
                    static bool s_block = false;
                    return s_block;
                }
            public:
                static void Lock()
                {
                    GetLock() = true;
                }
                static void Unlock()
                {
                    GetLock() = false;
                }
                static bool IsLocked()
                {
                    return GetLock();
                }
            };

            namespace Wrapper
            {
                template<class T>
                class Type : public T
                {
                public:
                    virtual ~Type()
                    {
                        IsDestroyed() = true;
                    }

                    static bool & IsDestroyed()
                    {
                        static bool s_destroyed = false;
                        return s_destroyed;
                    }
                };
            } /// end of namespace Wrapper
        } /// end of namespace Base

        template <typename DataT>
        class Type : public ::Wiz::NISingleton::Base::Type
        {
            typedef ::Wiz::NISingleton::Base::Wrapper::Type<DataT> tManagedData;
        private:
            static tManagedData& GetInstance()
            {
                static tManagedData ms_Singleton;

                return ms_Singleton;
            }
        public:
            static DataT& SingletonRef()
            {
                return GetInstance();
            }
            static DataT* SingletonPtr()
            {
                return &GetInstance();
            }
            static bool IsDestroyed()
            {
                return GetInstance().IsDestroyed();
            }
        };
    } /// end of namespace NISingleton
} /// end of namespace Wiz

#endif /*__WIZ_BASE_UTIL_SINGLETON_NON_INSTRUSIVE_HPP__SHANHAOBO_19800429__*/
